/******************************************************************
 * Created by: DO HOANG HAI                                       
 * Date of Creation: 18/Aug/2007                                    
 * Date of last modification: 22/Feb/2008
 *                                                                
 * Description: Main form of the program
 ******************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using Pbdms.Common;
using Pbdms.Common.DataObject;
using System.Collections;
using System.Threading;


namespace Pbdms.Forms

{
    public partial class FrmMain : Form
    {
        public ArrayList arlOfMissingFiles = new ArrayList();
        private bool isMinimized = false;

        public FrmMain()
        {
            InitializeComponent();
            //application setting
            //this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
            //check the library for errors.
            
            //this.Cursor = System.Windows.Forms.Cursors.Default;
                        
            
            Pbdms.Common.Global.paintBackGround(this);
            //involve a thread to listen for request from the network.
            this.Resize += new EventHandler(FrmMain_Resize);           
            
        }
        protected override void OnFormClosed(FormClosedEventArgs e)
        {            
            base.OnFormClosed(e);
            if (!isMinimized)
            {
                Program.exit();
            }
            
        }
        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (!isMinimized)
            {
                int result = Common.Global.showMsgTwoButtons("Are you sure to exit the program?\r\nWarning: The program will close immediatetly and discard all your work after you click 'Yes'", Common.GUI.MsgBoxTwoButtons.YES_NO, 'W');
                if (result == Common.GUI.MsgBoxTwoButtons.DIALOG_RESULT_NO)
                    e.Cancel = true;
                else
                {
                    base.OnFormClosing(e);                    
                }
            }
            else
            {   
                base.OnFormClosing(e);
            }            
        }

        void FrmMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                isMinimized = true;
                Program.notifyIcon1.Visible = true;
                this.ShowInTaskbar = false;
                this.Visible = false;
            }
            else
            {
                isMinimized = false;
            }
        }
        
        /// <summary>
        /// after the main form is rendered, the program should immediately check the library
        /// </summary>
        /// <param name="e"></param>
        protected override void  OnShown(EventArgs e)
        {
            base.OnShown(e);
            //check the library for any files missing
            checkLibraryForFilesMissing("Pbdms - Missing Files in the library");
            
        }      

        private void checkLibraryForFilesMissing(String title)
        {
            //check library
            arlOfMissingFiles = new ArrayList();
            if (hasFileMissingInTheLibrary(ref arlOfMissingFiles) == true)
            {
                if (arlOfMissingFiles!=null && arlOfMissingFiles.Count > 0)
                {
                    DisplayMissingFiles(title);
                }
            }
        }

        
        /// <summary>
        /// Display a list of missing files
        /// </summary>
        private void DisplayMissingFiles(String title)
        {            
            Common.Global.showErrorReporter(arlOfMissingFiles, title);
        }

        private bool hasFileMissingInTheLibrary(ref ArrayList arl2)
        {
            bool result = false;
            ArrayList arlItemIDs = BusinessLogic.DataController.ItemController.create().getListOfItemIDs();
            if (arlItemIDs !=null && arlItemIDs.Count>0)
            {
                foreach (object obj in arlItemIDs)
                {
                    FileInfo fileInfoObj = BusinessLogic.DataController.FileInfoController.create().getFileInfoObject(obj.ToString());

                    String fileUrl = fileInfoObj.Location + "\\" + fileInfoObj.FileName;

                    if (System.IO.File.Exists(fileUrl))
                    {
                        //update existence status if it was false.
                        if (fileInfoObj.Existence == false)
                            fileInfoObj.Existence = true;
                        BusinessLogic.DataController.FileInfoController.create().updateFileInfoToDB(fileInfoObj);
                    }
                    else
                    {
                        //if the file doesn't exist but the status is "existing"
                        if (fileInfoObj.Existence == true)
                        {
                            arl2.Add(fileUrl);
                            fileInfoObj.Existence = false;
                            fileInfoObj.Size = 0;
                            BusinessLogic.DataController.FileInfoController.create().updateFileInfoToDB(fileInfoObj);
                            result = true;
                        }
                    }

                    //review file
                    Comment commentObj = BusinessLogic.DataController.CommentController.create().getCommentFromDB(obj.ToString());
                    if (!commentObj.ReviewFile.Equals(""))
                    {
                        if (!System.IO.File.Exists(commentObj.ReviewFile))
                        {
                            arl2.Add(fileUrl);
                            commentObj.ReviewFile = "";
                            BusinessLogic.DataController.CommentController.create().updateCommentToDB(commentObj);
                            result = true;

                        }
                    }
                }
            }
            return result;
        }

        

        private void mn0204_Click(object sender, EventArgs e)
        {
            viewLibrary();    
        }

        private void mn0102_Click(object sender, EventArgs e)
        {
            this.Close();
            Program.exit(); 
        }

        private void mn0201_Click(object sender, EventArgs e)
        {
            bool alreadyExist = false;
            foreach (Form frm in (this.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Views.FrmBuddyList)))
                {    
                    frm.Activate();                  
                    alreadyExist= true;
                    break;
                }
            }
            if (!alreadyExist)
            {            
            Pbdms.Forms.Views.FrmBuddyList frmBuddy = new Pbdms.Forms.Views.FrmBuddyList();
            frmBuddy.MdiParent = this;
            frmBuddy.Show();
            }
        }

        private void mn0202_Click(object sender, EventArgs e)
        {
            bool alreadyExist = false;
            foreach (Form frm in (this.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Views.FrmConnection)))
                {
                    frm.Activate();
                  
                    alreadyExist = true;
                    break;
                }
            }
            if (!alreadyExist)
            {
                Pbdms.Forms.Views.FrmConnection frmCnn = new Pbdms.Forms.Views.FrmConnection();
                frmCnn.MdiParent = this;
                frmCnn.Show();
            }            
        }

        private void mn0403_Click(object sender, EventArgs e)
        {
            Pbdms.Forms.Help.frmAbout frmAbt = new Pbdms.Forms.Help.frmAbout();
            frmAbt.ShowDialog();
        }

        //private void mn0101_Click(object sender, EventArgs e)
        //{
        //    //Pbdms.Forms.Application.FrmPreference frmPreference = new Pbdms.Forms.Application.FrmPreference();
        //    //frmPreference.MdiParent = this;

        //    //frmPreference.Show();

        //    bool alreadyExist = false;
        //    foreach (Form frm in (this.MdiChildren))
        //    {
        //        frm.WindowState = FormWindowState.Normal;
        //        if (frm.GetType().Equals(typeof(Pbdms.Forms.Application.FrmPreference)))
        //        {                    
        //            frm.Activate();
        //            alreadyExist = true;
        //            break;
        //        }
        //    }
        //    if (!alreadyExist)
        //    {
        //        Pbdms.Forms.Application.FrmPreference frmPreference = new Pbdms.Forms.Application.FrmPreference();
        //        frmPreference.MdiParent = this;
        //        frmPreference.Show();
        //    }
        //}

        private void mn0302_Click(object sender, EventArgs e)
        {
            newSearch();
        }

        private void newSearch()
        {
            bool alreadyExist = false;
            foreach (Form frm in (this.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Tools.FrmSearch)))
                {
                    frm.Activate();
                    alreadyExist = true;
                    break;
                }
            }
            if (!alreadyExist)
            {
                Pbdms.Forms.Tools.FrmSearch frmSearch = new Pbdms.Forms.Tools.FrmSearch();
                frmSearch.MdiParent = this;
                frmSearch.Show();
            }
        }

        private void mn0301_Click(object sender, EventArgs e)
        {
            newAdd();
        }

        private void newAdd()
        {
            bool alreadyExist = false;
            foreach (Form frm in (this.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Tools.FrmAddItem)))
                {
                    frm.Activate();
                    alreadyExist = true;
                    break;
                }
            }
            if (!alreadyExist)
            {
                Pbdms.Forms.Tools.FrmAddItem frmSerach = new Pbdms.Forms.Tools.FrmAddItem();
                frmSerach.MdiParent = this;
                frmSerach.WindowState = FormWindowState.Normal;
                frmSerach.Show();
            }
        }

        private void btnLibrary_Click(object sender, EventArgs e)
        {
            viewLibrary();
        }

        private void viewLibrary()
        {
            bool alreadyExist = false;
            foreach (Form frm in (this.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Views.FrmLibrary)))
                {
                    frm.WindowState = FormWindowState.Normal;
                    frm.Activate();
                    alreadyExist = true;
                    break;
                }
            }
            if (!alreadyExist)
            {
                Pbdms.Forms.Views.FrmLibrary frmlib = new Pbdms.Forms.Views.FrmLibrary();
                frmlib.MdiParent = this;
                frmlib.Show();
            }
        }

        private void tStBtnAdd_Click(object sender, EventArgs e)
        {
            newAdd();
        }

        private void tStBtnSearch_Click(object sender, EventArgs e)
        {
            newSearch();
        }

        private void mn0205_Click(object sender, EventArgs e)
        {
            if (arlOfMissingFiles.Count > 0)
            {
                DisplayMissingFiles("Pbdms - Missing Files in the library");
            }
            else
            {
                Common.Global.showMsgOneButton("There is no missing file in the library",'I');
            }
        }

        

        private void mn0401_Click(object sender, EventArgs e)
        {
            string filePath = Common.Global.startupPath() +"\\Documents\\User Guide.pdf";
            if (System.IO.File.Exists(filePath))
            {
                try
                {
                    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(filePath);
                    psi.UseShellExecute = true;
                    System.Diagnostics.Process.Start(psi);
                }
                catch
                {
                    Common.Global.showMsgOneButton("Unable to open the file, please check the file path", 'C');
                }
            }
        }

        private void mn0303_Click(object sender, EventArgs e)
        {
            bool alreadyExist = false;
            foreach (Form frm in (this.MdiChildren))
            {
                frm.WindowState = FormWindowState.Normal;
                if (frm.GetType().Equals(typeof(Pbdms.Forms.Tools.FrmBrowse)))
                {
                    frm.WindowState = FormWindowState.Normal;
                    frm.Activate();
                    alreadyExist = true;
                    break;
                }
            }
            if (!alreadyExist)
            {
                Pbdms.Forms.Tools.FrmBrowse frmBrowse = new Pbdms.Forms.Tools.FrmBrowse();
                frmBrowse.MdiParent = this;
                frmBrowse.Show();
            }
        }
    }
}